package org.chen.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class PasswordUtil {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Value("${password.salt}")
    private String salt;

    public String encode(String password) {
        String newPass = salt + password;
        return passwordEncoder.encode(newPass);
    }

    public boolean matches(String rawPassword, String encodedPassword) {
        String newPass = salt + rawPassword;
        return passwordEncoder.matches(newPass, encodedPassword);
    }
}
